!function () {
    layui.config({base: '/layui/lay/modules/'}).use(['table', 'form','dateformat','common'], function () {
        //定义
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            dateformat = layui.dateformat,
            laydate = layui.laydate,
            common = layui.common;//自定义模块

        //定义总变量
        var HelpType = {
            tableId: "helpTypeTable",    //表格id
            condition: {                //查询条件
            },
        }

        /**
         * 初始化列表列
         * **/
        HelpType.initColumn = function(){
            return [[
                /*{type: 'checkbox', fixed: 'left'},*/
                {field: 'helpId', title: 'ID', width: 80,hide:true},
                {field: 'helpName', title: '分类名称', width: 360},
                {field: 'helpSum', title: '帮助数量', width: 160},
                {field: 'sort' ,title :'分类排序',width:120},
                {field: 'createTime' ,title :'发布时间',width: 165/*, templet: function (row) {
                        return dateformat.format(common.NewDate(row.createTime), 'yyyy-MM-dd hh:mm:ss');
                    }*/},
                {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 280, align: 'center'}
            ]];
        }


        /**
         * 查询方法
         * **/
        HelpType.search = function(){
            var queryData = {};
            table.reload(HelpType.tableId, {where: queryData,page: {curr: 1}});
        }

        /**
         * 新增记录
         * **/
        HelpType.addRecord =function (){

            //初始化表单
            $('#editForm')[0].reset();
            $('#editForm :input').val("");//重置所有的input
            //打开弹出层
            layer.open({
                type: 1, maxmin: true, title: '新增分类', shade: 0.4, shadeClose: false,
                area: ['640px', '280px'], content: $('#editForm').show(), btn: ['保存', '取消'],
                yes: function () {
                    $('#saveRecord').click();
                }, btn2: function () {
                    layer.closeAll();
                }, end: function () {
                    $('#editForm').hide();
                }
            });
        }

        /**
         * 修改记录
         * **/
        HelpType.editRecord =function (data){
            //初始化数据
            $.ajax({
                async: false,
                url: '/help/findHelpType?id=' + data.helpId,
                success: function (result) {
                    if (result && result.code == 200) {
                        var data = result.data;
                        form.render('radio').val('editForm', data);
                        //打开弹出层
                        layer.open({
                            type: 1, maxmin: true, title: '修改分类', shade: 0.4, shadeClose: false,
                            area: ['640px', '280px'], content: $('#editForm').show(), btn: ['保存', '取消'],
                            yes: function () {
                                $('#saveRecord').click();
                            }, btn2: function () {
                                layer.closeAll();
                            }, end: function () {
                                $('#editForm').hide();
                            }
                        });
                    }else
                        layer.alert(result.msg || '请求失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
        }

        /**
         * 删除记录
         * **/
        HelpType.deleteRecord =function (data){
            $.ajax({
                async: false,
                type: 'post',
                url: '/help/delete?id=' + data.helpId,
                success: function (result) {
                    if (result && result.code == 200) {
                        layer.closeAll();
                        layer.alert('操作成功！', {icon: 1, title: '提示'});
                        HelpType.search();
                    }
                },
                error: function () {
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
        }


        table.render({
            elem:"#helpTypeTable"
            ,url:"/help/listPage"
            ,cellMinWidth:80
            ,request: {pageName: 'pageNum', limitName: 'pageSize'}
            ,page: true
            ,cols:HelpType.initColumn(),
            cellMinHeight:200

        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            HelpType.addRecord();
        });

        // 返回点击事件
        $('#btnBack').click(function () {
            window.location.href = "/assistInfo/list";
        });

        //自定义验证规则
        form.verify({
            tagLen: [
                /^[\d\D]{0,16}$/
                ,'帮助分类名称最多输入16个字'
            ],
            textLen:[
                /^[\d\D]{0,200}$/
                ,'最多输入200个字符'
            ],
        });

        //监听工具条
        table.on('tool(' + HelpType.tableId + ')',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if( layEvent ==='detail'){
                //查看
                return false;
            } else if( layEvent ==='del'){
                layer.confirm('确定要删除该帮助分类吗？', function (index) {
                    HelpType.deleteRecord(data);
                    return false;
                });
            } else if( layEvent ==='edit'){
                HelpType.editRecord(data);
                return false;
            }
        });

        //监听提交事件
        form.on('submit(editRecord)',function(data){

            //通过from提交  包含图片
            var formdata  = new FormData(data.form);
            var postUrl = !data.field['helpId'] ? '/help/addHelpType' : '/help/editHelpType';

            $.ajax({
                async: false, type: 'post', traditional: true, data: formdata, url: postUrl,
                contentType:false,processData:false,
                success: function (data) {
                    if (data && data.code == 200) {
                        layer.closeAll();
                        layer.alert('操作成功！', {icon: 1, title: '提示'});
                        HelpType.search();
                    } else
                        layer.alert(data.msg || '操作失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.closeAll();
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
            return false;
        });

    });
}();